import EventManager from "../Managers/EventManager";
import { EventTypes } from "../Managers/EventTypes";
import { AudioSystem } from "../SystemAudio/AudioSystem";
import { clog } from "../Tools/ColorLog";


export default class SDK {
    protected api = null;

    public init(callback?: Function, target?: any) {
        this.onEvents();

        this.initSdk(callback, target);
    }

    // 监听事件
    protected onEvents() {
        EventManager.on(EventTypes.SDKEvents.ShowBanner, this.showBanner, this);
        EventManager.on(EventTypes.SDKEvents.HideBanner, this.hideBanner, this);
        EventManager.on(EventTypes.SDKEvents.ShowVideo, this.showRewardedVideo, this);
        EventManager.on(EventTypes.SDKEvents.ShowInsertAd, this.showInterstitial, this);
        EventManager.on(EventTypes.SDKEvents.ShowCustomAd, this.showCustomAd, this);
        EventManager.on(EventTypes.SDKEvents.HideCustomAd, this.hideCustomAd, this);
        EventManager.on(EventTypes.SDKEvents.Share, this.share, this);
        EventManager.on(EventTypes.SDKEvents.ExitApp, this.onExitApp, this);
    }

    /**
    * 初始化SDK，操作所在SDK接口之前必须对SDK进行初始化 
    */
    protected setAdCfg() {

    }

    private initSdk(callback?: Function, target?: any) {
        if (callback) callback.call(target);
    }

    //#region ----------------功能---------
    //提示消息
    protected showMessage(msg: string) {
        EventManager.emit(EventTypes.GameEvents.ShowTips, msg);
    }

    //#endregion

    // #region -----------------事件---------------
    /**
       * 显示 Banner 广告
       */
    protected showBanner() {
        console.log("showBanner");
    }

    /**
     * 手动关闭 Banner 广告
     */
    protected hideBanner() {
        console.log("hideBanner");
    }

    /**
     * 拉起激励视频广告
     */
    protected showRewardedVideo(cb: Function | { success: Function, fail: Function, cancel: Function }) {
        let success = null;
        let fail = null;
        let cancel = null;
        if (typeof cb === 'object') {
            success = cb.success;
            fail = cb.fail;
            cancel = cb.cancel;
        } else {
            success = cb;
        }
        //暂停游戏
        EventManager.emit(EventTypes.GameEvents.GamePause);
        AudioSystem.pauseBGM();
        //
        console.warn('观看视频中');
        success && success();
    }

    /**
     * 插屏广告
     */
    protected showInterstitial(cb1?, cb2?) {
        console.log("showInterstitial");
        cb1 && cb1();

    }

    /**
     * 显示原生模板广告
     */
    protected showCustomAd(adIndex: number) {
        console.log("showCustomAd");

    }

    /**
     * 手动关闭原生模板广告
     */
    protected hideCustomAd(adIndex: number) {
        console.log("hideCustomAd");

    }

    /**
     * 自定义分享
     */
    protected share() {
        console.log("share");
    }
    // #endregion

    //退出游戏
    protected onExitApp() {

    }
}
